package com.keyancompany.zhongbaopingtai.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.listener.LoadingHanlder;
import com.keyancompany.zhongbaopingtai.utils.NetUtils;
import com.keyancompany.zhongbaopingtai.utils.T;

import butterknife.ButterKnife;

/**
 * Created by firefox on 2016/5/31.
 */
public abstract class BaseFragment extends Fragment {
    protected View contentView;
    protected Context context;
    private LoadingHanlder handler;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = View.inflate(getActivity(), getContentViewId(), null);
        ButterKnife.bind(this, contentView);
        context = getActivity();
        setUpViews();
        return contentView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public void initSwipeLayout(SwipeRefreshLayout swipeLayout) {
        swipeLayout.setColorSchemeResources(R.color.colorPrimary,
                R.color.colorPrimaryDark);

        swipeLayout.setSize(SwipeRefreshLayout.LARGE);

        swipeLayout.setProgressBackgroundColor(R.color.colorAccent);
        swipeLayout.setSize(SwipeRefreshLayout.DEFAULT);


        swipeLayout.setProgressViewEndTarget(true, 300);
    }

    public abstract int getContentViewId();

    public abstract void setUpViews();


    boolean isNetvaiable() {
        if (!NetUtils.isConnected(getActivity())) {
            T.showShort(getActivity(), "请检查网络~");
            return false;
        }
        return true;
    }

    public LoadingHanlder getLoading(int hint) {
        if (handler == null)
            handler = new LoadingHanlder(getActivity());
        if (hint != -1)
            handler.setTitleId(hint);
        return handler;
    }

}
